#include "mqtt.h"
#include "ui_mqtt.h"
#include "config.h"
Mqtt::Mqtt(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Mqtt)
{
    ui->setupUi(this);
    ui->lineEdit_ip->setText(MQTT_HOSTNAME);
    ui->lineEdit_port->setText(QString::number(MQTT_PORT));
    ui->lineEdit_pub->setText(MQTT_DEVICE_SIM);
    ui->lineEdit_sub->setText(MQTT_DEVICE_APP);
}

Mqtt::~Mqtt()
{
    delete ui;
}

void Mqtt::on_pushButton_connect_clicked()
{
    client = new QMqttClient;
    //绑定连接成功信号
    connect(client, &QMqttClient::connected, this, &Mqtt::connectSuccess);
    connect(client, &QMqttClient::disconnected, this, &Mqtt::connectError);
    //绑定连接的ip
    client->setHostname(ui->lineEdit_ip->text());
    //绑定端口
    client->setPort(ui->lineEdit_port->text().toInt());
    //连接服务器
    client->connectToHost();
}

void Mqtt::connectSuccess()
{
    QMessageBox::about(this, "提示", "连接成功");
}

void Mqtt::connectError()
{
    QMessageBox::about(this, "提示", "连接失败");
}

void Mqtt::on_pushButton_subscribe_clicked()
{
    //订阅主题
    client->subscribe(ui->lineEdit_sub->text());
}

void Mqtt::on_pushButton_subscribe_2_clicked()
{
    //返回
    this->close();
}

